global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A40_wages_and_deflators.log, replace name(tabl)

capture noi {

	qui do ${code_dir}/config/tabletools.do

	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
		
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	sort lse_id year

	estimates clear
	foreach XD in LPm DPm MGm MPt DPt{
		clonevar LSW = lsw`XD'_1995_a
		clonevar HSW = hsw`XD'_1995_a
		clonevar VAEMP = vaemp`XD'_1995_a
		clonevar GDPGAP = lngdpgap_1995_a
		clonevar GDPPC = gdppc`XD'_1995_a
		clonevar GDPPC_foreign = gdppc`XD'_shr4_foreign_1995_a
		clonevar VAEMP_foreign = vaemp`XD'_shr4_foreign_1995_a
		clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
		clonevar LSW_foreign = lsw`XD'_shr4_foreign_1995_a
		clonevar HSW_foreign = hsw`XD'_shr4_foreign_1995_a
		
		ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store col`XD'
		drop LSW HSW VAEMP GDPGAP GDPPC *_foreign
	}

	setlabels

	local deflator "{Deflator} & {\makecell{Manufacturing PPI, \\ conversion in 2005}} & {\makecell{US manufacturing PPI, \\ conversion every year}} & {\makecell{GDP deflator, \\ conversion in 1995}} & {\makecell{Manufacturing PPI, \\ conversion in 1995}} & {\makecell{US manufacturing PPI, \\ conversion every year}} \\"

	dhoztab * using ${tab_dir}/appendix/Table_A40_wages_and_deflators.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label drop(*zero _cons stock* spill*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) substitute(\_ _) ///
		depvar("Auto95") refcat(LSW "Foreign:", nolabel) ///
		rawaddheader({Sector}&\multicolumn{3}{c}{Manufacturing}&\multicolumn{2}{c}{Total}\\ \cmidrule(lr){2-4}\cmidrule(lr){5-6}`deflator') ///
		notes("This table shows robustness to different wage conversions. Columns 1--3 use manufacturing wages and columns 4 and 5 total wages. In column 1, macroeconomic variables are deflated with the local manufacturing PPI and converted to USD in 2005. In Columns 2 and 5 they are converted to USD every year and deflated with the US manufacturing PPI. In Column 3, macroeconomic variables are deflated with the local GDP deflator and converted to USD in 1995. In Column 4, macroeconomic variables are deflated with the local manufacturing PPI and converted to USD in 1995.  All regressions include firm fixed effects, industry-year fixed effects and country-year fixed effects. In all columns, the macroeconomic variables are the normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.  ")
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}